/**
 * SpeakMessageService for Android.
 * 
 * Copyright 2009 Daniel Foote
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cz.muni.fi.android.assistivecamera.texttospeech;

import java.util.List;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

public class ScreenReader {
	private final static String SCREENREADER_INTENT_ACTION = "android.accessibilityservice.AccessibilityService";

    private final static String SCREENREADER_INTENT_CATEGORY = "android.accessibilityservice.category.FEEDBACK_SPOKEN";
	
	public static boolean isRunning(Context context) {
        // Restrict the set of intents to only accessibility services that have
        // the category FEEDBACK_SPOKEN (aka, screen readers).
        Intent screenReaderIntent = new Intent(SCREENREADER_INTENT_ACTION);
        screenReaderIntent.addCategory(SCREENREADER_INTENT_CATEGORY);
        List<ResolveInfo> screenReaders = context.getPackageManager().queryIntentServices(
                screenReaderIntent, 0);
        ContentResolver cr = context.getContentResolver();
        Cursor cursor = null;
        int status = 0;
        for (ResolveInfo screenReader : screenReaders) {
            // All screen readers are expected to implement a content provider
            // that responds to
            // content://<nameofpackage>.providers.StatusProvider
        	StringBuilder query = new StringBuilder("content://");
        	query.append(screenReader.serviceInfo.packageName);
        	query.append(".providers.StatusProvider");
        	Log.i("ScreenReader", query.toString());
            cursor = cr.query(Uri.parse(query.toString()), null, null, null, null);
            if (cursor != null) {
                if(cursor.moveToFirst()){
                	// These content providers use a special cursor that only has one element, 
                // an integer that is 1 if the screen reader is running.
                
                	status = cursor.getInt(1);
                	cursor.close();
                	if (status == 1) {
                		return true;
                	}
                }
                if(cursor.getCount()>0){
                	status=cursor.getInt(0);
                	cursor.close();
                	if (status == 1) {
                		return true;
                	}	
                }
            }
        }
        return false;
    }
}
